package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.MeMoralEducationRecordJdbc;
import com.wuseguang.springcloud.dao.repository.MeMoralEducationRecordRepository;
import com.wuseguang.springcloud.dao.repository.MeMoralEducationRepository;
import com.wuseguang.springcloud.entity.MeMoralEducation;
import com.wuseguang.springcloud.entity.MeMoralEducationRecord;
import com.wuseguang.springcloud.service.MeMoralEducationRecordService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 德育-班级德育记录 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-06-28
 */
@Service
public class MeMoralEducationRecordServiceImpl  implements MeMoralEducationRecordService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private MeMoralEducationRecordRepository meMoralEducationRecordRepository;
    @Autowired
    private MeMoralEducationRecordJdbc meMoralEducationRecordJdbc;
    @Autowired
    private MeMoralEducationRepository meMoralEducationRepository;

    @Override
    public Result addMeMoralEducationRecord(MeMoralEducationRecord meMoralEducationRecord) {
        logger.info("添加班级德育记录");
        meMoralEducationRecord.setAddTime(new Date());
        meMoralEducationRecordRepository.save(meMoralEducationRecord);
        return new Result();
    }

    @Override
    public Result findMeMoralEducationRecord(String schoolId, Integer findType, String startTime, String endTime, String classId, Integer indexNo, Integer rows) {
        logger.info("添加班级德育记录");
        //查询时间
        String time = "";

        //判断查询类型
        if (findType == 1) {
            logger.info("查询上周数据");
            time = " and YEARWEEK(date_format(mer.add_time,'%Y-%m-%d')) = YEARWEEK(now())-1 ";
        } else if (findType == 2) {
            logger.info("查询本月数据");
            time = " and date_format(mer.add_time,'%Y-%m')=date_format(now(),'%Y-%m') ";
        } else if (findType == 3) {
            logger.info("查询自定义数据");
            time = " and date_format(mer.add_time,'%Y-%m-%d')  between date_format('" + startTime + "','%Y-%m-%d') and date_format('" + endTime + "','%Y-%m-%d') ";
        } else {
            logger.info("查询本周数据");
            time = " and YEARWEEK(date_format(mer.add_time,'%Y-%m-%d')) = YEARWEEK(now()) ";
        }

        //查询数据
        String sql = " select mer.id,mer.`explains`,DATE_FORMAT(mer.add_time,'%Y-%m-%d %T') add_time ,mer.demerit,me.`name`,mer.teacher_name from  " +
                " me_moral_education_record mer left join me_moral_education me on(me.id=mer.moral_education_id)  where  mer.school_id= ?" +
                " "+ (StringUtils.isEmpty(classId)? " ": " and mer.class_id = "+classId+" ")+ time +" ORDER BY id desc ";

        List<Map<String,Object>> list = meMoralEducationRecordJdbc.queryListMap(sql,schoolId);
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        //返回数据
        return new Result(new PaginationUtil<>(list.size(),rows, tool.getList(), indexNo));
    }

    @Override
    public Result deleteMeMoralEducationRecord(Integer id) {
        meMoralEducationRecordRepository.deleteById(id);
        return new Result();
    }
}
